// Article page waitlist modal functionality
function initArticleWaitlistModal() {
    const waitlistBtn = document.getElementById('article-waitlist-btn');
    const waitlistModal = document.getElementById('waitlist-modal');
    const closeModal = waitlistModal ? waitlistModal.querySelector('.close') : null;
    const waitlistForm = document.getElementById('article-waitlist-form');
    
    // Open modal
    if (waitlistBtn && waitlistModal) {
        waitlistBtn.addEventListener('click', () => {
            waitlistModal.style.display = 'block';
            document.body.style.overflow = 'hidden';
        });
    }
    
    // Close modal
    if (closeModal && waitlistModal) {
        closeModal.addEventListener('click', () => {
            waitlistModal.style.display = 'none';
            document.body.style.overflow = 'auto';
        });
    }

    // Close modal when clicking outside
    if (waitlistModal) {
        window.addEventListener('click', (e) => {
            if (e.target === waitlistModal) {
                waitlistModal.style.display = 'none';
                document.body.style.overflow = 'auto';
            }
        });
    }

    // Handle form submission
    if (waitlistForm) {
        waitlistForm.addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const emailInput = waitlistForm.querySelector('input[type="email"]');
            const email = emailInput.value;
            const submitBtn = waitlistForm.querySelector('button[type="submit"]');
            
            // Disable form during submission
            submitBtn.disabled = true;
            const originalText = submitBtn.textContent;
            submitBtn.textContent = 'Submitting...';
            
            try {
                // Use Formspree service
                const response = await fetch('https://formspree.io/f/mqabrgep', {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ 
                        email: email,
                        message: 'New waitlist signup from article page',
                        source: 'Article Page'
                    })
                });
    
                if (response.ok) {
                    alert("Thanks – we'll be in touch!");
                    emailInput.value = '';
                    waitlistModal.style.display = 'none';
                    document.body.style.overflow = 'auto';
                } else {
                    throw new Error('Submission failed');
                }
            } catch (error) {
                console.error('Waitlist submission error:', error);
                alert('Submission failed, please try again later');
            } finally {
                // Re-enable form
                submitBtn.disabled = false;
                submitBtn.textContent = originalText;
            }
        });
    }
}

// Escape key to close modal
document.addEventListener('keydown', (e) => {
    const waitlistModal = document.getElementById('waitlist-modal');
    if (e.key === 'Escape' && waitlistModal && waitlistModal.style.display === 'block') {
        waitlistModal.style.display = 'none';
        document.body.style.overflow = 'auto';
    }
});

// Initialize on page load
document.addEventListener('DOMContentLoaded', () => {
    initArticleWaitlistModal();
}); 